import QtQuick 2.3
import QtQuick.Window 2.2
import QtQuick.Controls 1.4
import QtQuick.Dialogs 1.2
import QtQuick.Controls.Styles 1.4

Window {
    id:mainwindow;
    width: Screen.width*2/5;
    height:Screen.height/2;

    //flags: Qt.FramelessWindowHint | Qt.Window;

    visible: true
    property int nItemCount: 0;
    property int nCurrMenu: -1;

    property var btnComponent;

    MouseArea{
        anchors.fill: parent;

        onPressed: {
            if(menuBtn.focus)
            {
                menuBtn.focus=false;

            }
        }
    }

    Component{
        id:compButton;
        Item {
            id:menuButton
            //anchors.fill: parent;

            property string strWinName : "";
            property string strFile : "";
            property int nMenuLoad: -1;
            property var loader;
            property bool isAvailable:true;

            Button{
                id:btn;
                anchors.fill: parent;
                //anchors.margins: 1;

                z:1
                //text:strWinName;
                anchors.bottomMargin: 3;               

                Text {
                    id: btnTxt
                    text: qsTr(strWinName);

                    anchors.centerIn: parent;
                    //anchors.fill: parent;
                    anchors.horizontalCenter: parent.horizontalCenter;
                    anchors.verticalCenter: parent.verticalCenter;
                    //wrapMode: Text.Wrap;

                    elide: Text.ElideRight;
                }

                onClicked: {
                    for(var j=0;j<6;j++)
                    {
                        if(btnComponent[j].nMenuLoad === nMenuLoad)
                        {
                            mainwindow.nCurrMenu = j;
                            break;
                        }
                    }

                    //mainwindow.nCurrMenu = nMenuLoad;

                    menuButton.focus = true;

                    for(var i=0;i<6;i++)
                    {
                        if(btnComponent[i].isAvailable === false)
                        {
                            btnComponent[i].loader.visible =false;                            
                        }
                    }

                    loader.visible =true;
                }

                style: ButtonStyle{
                    background:Rectangle{
                        implicitWidth: btn.width;
                        implicitHeight: btn.height;

                        radius: 3;
                        //color:(menuButton.focus?"#e58585":"#888888");

                        color:((mainwindow.nCurrMenu >-1 && btnComponent[mainwindow.nCurrMenu].nMenuLoad === nMenuLoad)?"#e58585":"#888888");

                    }
                }
            }
        }
    }

    Rectangle{

        anchors.fill: parent;
        anchors.margins: 2;

        Column{
            anchors.fill: parent;

            Row{
                id:toprow;
                width: parent.width;
                height: 40

                spacing: 1
                Rectangle{
                    id:menuBtn;
                    height: toprow.height;
                    width: 60;

                    border.color: "#e8e8e8";
                    border.width: 1;

                    Text {
                        id: txtMenu
                        text: qsTr("IDEA")
                        anchors.centerIn: parent;

                        font.pixelSize : 14;
                        font.bold: true;
                    }

                    MouseArea{
                        anchors.fill: parent;
                        onPressed: {
                            if(menuBtn.focus)
                            {
                                menuBtn.focus=false;

                                for(var i=0;i<6;i++)
                                {
                                    if(btnComponent[i].nMenuLoad === nCurrMenu)
                                    {
                                        btnComponent[i].focus=true;
                                        break;
                                    }
                                }
                            }else
                            {
                                menuBtn.focus=true;

                                loaderMenu.visible=true;
                                menuBtn.color="lightblue";
                                txtMenu.color="white";
                            }
                        }
                    }

                    onFocusChanged: {
                        if(menuBtn.focus==false)
                        {
                            loaderMenu.visible=false;
                            menuBtn.color="white"
                            txtMenu.color="black";

                            if(mainwindow.nCurrMenu>=0)btnComponent[mainwindow.nCurrMenu].focus = true;
                        }
                    }
                }

                Rectangle{
                    id:statusRect;
                    height: toprow.height;
                    width: toprow.width-menuBtn.width-closeRect.width+2;

                    border.color: "#e8e8e8";
                    border.width: 1;
                }

                Rectangle{
                    id:closeRect;
                    height: toprow.height;
                    width: 50;

                    border.color: "#e8e8e8";
                    border.width: 1;

                    Image{
                        id:imageClose;

                        visible: false;
                        anchors.fill: parent;

                        source: "/Icon/Pro/Icon/Cancel.png"

                        MouseArea{
                            anchors.fill: parent;

                            onPressed: {
                                closeRect.color = "lightblue";
                            }

                            onReleased: {
                                closeRect.color = "white";
                            }

                            onClicked: {
                                loaderMenu.visible=false;
                                menuBtn.color="white";
                                menuBtn.focus = false;                               

                                var k;


                                for(var i=0;i<6;i++)
                                {
                                    if(i === mainwindow.nCurrMenu)
                                    //if(btnComponent[i].focus)
                                    {
                                        btnComponent[i].isAvailable = true;
                                        btnComponent[i].visible = false;
                                        btnComponent[i].loader.source = "";

                                        var nLoad=btnComponent[i].nMenuLoad;

                                        for(var j=0;j<6;j++)
                                        {
                                            if(btnComponent[j].isAvailable === false)
                                            {
                                                if(btnComponent[j].nMenuLoad>nLoad)
                                                {
                                                    btnComponent[j].nMenuLoad = btnComponent[j].nMenuLoad-1;
                                                    btnComponent[j].x = (bottomRect.width-6)/6 * (btnComponent[j].nMenuLoad)+btnComponent[j].nMenuLoad;
                                                }
                                            }else
                                            {
                                                btnComponent[j].nMenuLoad = -1;
                                            }
                                        }

                                        nItemCount = nItemCount-1;

                                        var InxTemp=-1;
                                        var IndxMax=-1;                                        

                                        for(k=0;k<6;k++)
                                        {
                                            if(btnComponent[k].isAvailable === false)
                                            {
                                                if(InxTemp<btnComponent[k].nMenuLoad)
                                                {
                                                    InxTemp=btnComponent[k].nMenuLoad;
                                                    IndxMax=k;
                                                }
                                            }

                                        }

                                        //mainwindow.nCurrMenu = nItemCount-1;

                                        if(nItemCount>0)
                                        {
                                            //midLoader.item.strPagesName=btnComponent[nItemCount-1].strWinName;
                                            //btnComponent[nItemCount-1].loader=btnComponent[nItemCount-1].strWinName;

                                            //btnComponent[k].loader.visible = false;

                                            for(var l=0;l<6;l++)
                                            {
                                                if(btnComponent[l].isAvailable === false)btnComponent[l].loader.visible = false;
                                            }
                                            btnComponent[IndxMax].loader.visible=true;
                                            btnComponent[IndxMax].focus=true;

                                            mainwindow.nCurrMenu = IndxMax;

                                        }
                                        else
                                        {
                                            //midLoader.source="";

                                            //btnComponent[nItemCount-1].loader="";
                                            imageClose.visible=false;

                                            mainwindow.nCurrMenu = -1;

                                        }


                                        break;
                                    }
                                }
                            }

                        }
                    }
                }

            }

            Row{
                id:midrow

                width: parent.width;
                height:mainwindow.height-toprow.height-bottomrow.height;

                Rectangle{

                    width: parent.width;
                    height: parent.height;

                    border.color: "lightgray";
                    border.width: 1;

                    color: "#f5f5f5";

                    Loader{
                        id:midLoader;
                        anchors.fill: parent;

                        //property bool isAvailable: true;

                        source: ""
                    }

                    Loader{
                        id:midLoader1;

                        //property bool isAvailable: true;

                        anchors.fill: parent;


                        visible: false;
                        source: ""
                    }

                    Loader{
                        id:midLoader2;
                        //property bool isAvailable: true;

                        anchors.fill: parent;

                        visible: false;
                        source: ""
                    }

                    Loader{
                        id:midLoader3;
                        //property bool isAvailable: true;

                        anchors.fill: parent;

                        visible: false;
                        source: ""
                    }

                    Loader{
                        id:midLoader4;
                        //property bool isAvailable: true;

                        anchors.fill: parent;

                        visible: false;
                        source: ""
                    }

                    Loader{
                        id:midLoader5;
                        //property bool isAvailable: true;

                        anchors.fill: parent;

                        visible: false;
                        source: ""
                    }

                    Loader{
                        id:midLoader6;
                        //property bool isAvailable: true;

                        anchors.fill: parent;

                        visible: false;
                        source: ""
                    }

                 }
            }

            Row{
                id:bottomrow;
                width: parent.width;
                height: 40

                spacing: 1

                Rectangle{
                    id:bottomRect;
                    width: parent.width-setrect.width;
                    height: parent.height;

                    border.color: "#e8e8e8";
                    border.width: 1;

                    signal menuItemAdd(string strPathName,string strFileName);

                    onMenuItemAdd: {

                        if(compButton.status === Component.Ready)
                        {
                            //btnComponent[nItemCount-1] = compButton.createObject(bottomRect,{"width":(bottomRect.width-6)/6,"height":bottomrow.height-4});

                            var loaderIndex=-1;

                            for(var i=0;i<6;i++)
                            {
                                if(btnComponent[i].isAvailable === true)
                                {
                                    loaderIndex=i;
                                    break;
                                }
                            }

                            btnComponent[loaderIndex].nMenuLoad = nItemCount-1;

                            btnComponent[loaderIndex].width = (bottomRect.width-6)/6
                            btnComponent[loaderIndex].height = bottomRect.height-4

                            btnComponent[loaderIndex].x = (bottomRect.width-6)/6 * (btnComponent[loaderIndex].nMenuLoad)+btnComponent[loaderIndex].nMenuLoad;
                            btnComponent[loaderIndex].y = 3;

                            //btnComponent[nItemCount-1].strWinName = midLoader.item.strPagesName;

                            if(midLoader1.sourceComponent == null)
                            {
                                btnComponent[loaderIndex].loader = midLoader1;

                            }else if(midLoader2.sourceComponent == null)
                            {
                                btnComponent[loaderIndex].loader = midLoader2;
                            }else if(midLoader3.sourceComponent == null)
                            {
                                btnComponent[loaderIndex].loader = midLoader3;
                            }else if(midLoader4.sourceComponent == null)
                            {
                                btnComponent[loaderIndex].loader = midLoader4;
                            }else if(midLoader5.sourceComponent == null)
                            {
                                btnComponent[loaderIndex].loader = midLoader5;
                            }else if(midLoader6.sourceComponent == null)
                            {
                                btnComponent[loaderIndex].loader = midLoader6;
                            }

                            for(var j=0;j<6;j++)
                            {
                                if(btnComponent[j].isAvailable === false)btnComponent[j].loader.visible = false;

                            }

                            btnComponent[loaderIndex].isAvailable=false;
                            btnComponent[loaderIndex].loader.visible = true;
                            btnComponent[loaderIndex].loader.source= strPathName + ".qml";
                            btnComponent[nItemCount-1].strWinName=strFileName;
                            mainwindow.nCurrMenu = loaderIndex;
                            btnComponent[loaderIndex].visible = true;
                            btnComponent[loaderIndex].focus = true;

                        }

                        if(nItemCount>0)imageClose.visible=true;
                    }
                }

                Rectangle{
                    id:setrect
                    width: 50;
                    height: parent.height;

                    border.color: "lightblue";
                    border.width: 1;
                }
            }
        }
    }

    Loader{
        id:loaderMenu;

        x:toprow.x;
        y:toprow.height;

        width: midrow.width-toprow.height;
        height: midrow.height;

        z:0.5;
        visible: false;
        source: "MenuRect.qml";

    }

    Connections{
        target:loaderMenu.item

        onSelectItem:{

            if(nItemCount<6)
            {
                nItemCount++;

                bottomRect.menuItemAdd(strPathName,strFileName);
            }else
            {
                btnComponent[mainwindow.nCurrMenu].focus = true;
            }

            menuBtn.focus=false;
        }
    }

    Component.onCompleted: {
        btnComponent = new Array;

        for(var i=0;i<6;i++)
        {
            btnComponent[i] = compButton.createObject(bottomRect,{"width":0,"height":0});

            //btnComponent[i].parent = bottomRect;
            //btnComponent[i].visible=false;
            btnComponent[i].isAvailable=true;
        }
    }

}

